package cn.ncupyh.soft.opera;

import cn.ncupyh.soft.book.Book;
import cn.ncupyh.soft.book.BookList;

import java.util.Scanner;

/**
 * @version 1.8
 * @Author Djx_hmbb
 * @Date 2023/7/22 9:52
 * @注释:
 */
public class AddOperation implements IOPeration{

    public void work(BookList bookList){
        System.out.println("新增图书!");
        System.out.println("请输入你要新增的书名:");
        Scanner s = new Scanner(System.in);
        String name = s.next();
        System.out.println("auhtor:");
        String author = s.next();
        System.out.println("age:");
        int age = s.nextInt();
        System.out.println("type:");
        String type = s.next();
        Book book = new Book(name,author,age,type);

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("已存在该书,无法添加!");
                return;
            }
        }
        bookList.setBook(book);
        bookList.setUsedSize(currentSize+1);
        System.out.println("添加成功!");
    }
}
